/**
 * 
 */
package br.com.rodolfo.timesheet.converter;

import java.sql.SQLException;

import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.com.rodolfo.timesheet.entity.Cargo;
import br.com.rodolfo.timesheet.service.CargoService;

/**
 * @author rjulio
 *
 */
@FacesConverter(value="cargoConverter", forClass=Cargo.class)
public class CargoConverter implements Converter {
	@ManagedProperty("#{cargoService}")
	private CargoService cargoService;

	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException  {
	
		try {
			return cargoService.findByName(value);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ConverterException(e.getMessage());
		}
	}

	public String getAsString(FacesContext context, UIComponent componente, Object object) {
		// TODO Auto-generated method stub
		if ( object instanceof Cargo){
			return  ((Cargo)object).getDescricao();
		}
		return null;
	}

}
